//
//  BookingViewController.m
//  Frame
//
//  Created by 这是工作本 on 16/1/15.
//  Copyright © 2016年 这是工作本. All rights reserved.
//

#import "BookingViewController.h"

@interface BookingViewController ()<UIGestureRecognizerDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *HcollectionV;
@property (nonatomic, retain) UIView *scrollView;
@property (nonatomic, retain) UICollectionView *BookcollectionV;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, assign) BOOL clickbutton;
@property (nonatomic, assign) BOOL backColor;
@property (nonatomic, retain) UIScrollView *scroV;
@property (nonatomic, retain) UIView *viewB;
@end

@implementation BookingViewController

- (void)viewWillAppear:(BOOL)animated
{

    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    self.navigationItem.title = @"选择场次";
    self.clickbutton = YES;
    self.backColor = NO;
    //设置title颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:1.0],NSForegroundColorAttributeName,nil]];
    
    //隐藏自动返回
    self.navigationItem.hidesBackButton = YES;
    
    //返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"but_an.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    
    //横向collectionView 时间
    UICollectionViewFlowLayout *flowL_H = [[UICollectionViewFlowLayout alloc] init];
    //最小行间距
    flowL_H.minimumLineSpacing = 10 * ScreenWidth;
    //滚动方向
    flowL_H.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //单位大小
    flowL_H.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 30 * ScreenWidth) / 4, 40 * ScreenHeigth);
    //与屏幕四边的间距
    flowL_H.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.HcollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 40 * ScreenHeigth) collectionViewLayout:flowL_H];
    //隐藏指示条
    self.HcollectionV.showsHorizontalScrollIndicator = NO;
    //关掉滑动
    self.HcollectionV.scrollEnabled = YES;
    //关闭自动偏移
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.HcollectionV.backgroundColor = [UIColor colorWithRed:240 / 255.0  green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
    self.HcollectionV.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width  / 4,  0);
    //签协议
    self.HcollectionV.delegate = self;
    self.HcollectionV.dataSource = self;
    //注册cell
    [self.HcollectionV registerClass:[TimeCollectionViewCell class] forCellWithReuseIdentifier:@"horizontalCell"];
    self.HcollectionV.bounces = NO;
    self.HcollectionV.pagingEnabled = YES;
    
    [self.view addSubview:self.HcollectionV];

    //滑条
    self.scrollView = [[UIView alloc] initWithFrame:CGRectMake(5 * ScreenWidth, 39 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width / 4 - 3 * 10 * ScreenWidth, 1 * ScreenHeigth)];
    self.scrollView.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:127 / 255.0 blue:140 / 255.0 alpha:1.0];
    [self.HcollectionV addSubview:self.scrollView];

    //时间
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, self.HcollectionV.frame.origin.y + self.HcollectionV.frame.size.height + 29 * ScreenHeigth, 100 * ScreenWidth, 20 * ScreenHeigth)];
    la.text = @"8:00";
    la.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:la];
    
    
    UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, la.frame.origin.y + la.frame.size.height + 30 * ScreenHeigth, 100 * ScreenWidth, 20 * ScreenHeigth)];
    la1.text = @"9:00";
    la1.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:la1];

    UILabel *la2 = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, la1.frame.origin.y + la1.frame.size.height + 30 * ScreenHeigth, 100 * ScreenWidth, 20 * ScreenHeigth)];
    la2.text = @"10:00";
    la2.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:la2];

    
    UILabel *la3 = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, la2.frame.origin.y + la2.frame.size.height + 30 * ScreenHeigth,  100 * ScreenWidth, 20 * ScreenHeigth)];
    la3.text = @"11:00";
    la3.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:la3];

    
    UILabel *la4 = [[UILabel alloc] initWithFrame:CGRectMake(20 * ScreenWidth, la3.frame.origin.y + la3.frame.size.height + 30 * ScreenHeigth, 100 * ScreenWidth, 20 * ScreenHeigth)];
    la4.text = @"12:00";
    la4.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:la4];
    
    //场地
    UILabel *area = [[UILabel alloc] initWithFrame:CGRectMake(70 * ScreenWidth, self.HcollectionV.frame.origin.y + self.HcollectionV.frame.size.height + 10 * ScreenHeigth , 100 * ScreenWidth, 20 * ScreenHeigth)];
    area.text = @"一号场地";
    area.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:area];

    
    UILabel *area1 = [[UILabel alloc] initWithFrame:CGRectMake(area.frame.origin.x + area.frame.size.width + 15 * ScreenWidth, area.frame.origin.y , area.frame.size.width, area.frame.size.height)];
    area1.text = @"二号场地";
    area1.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:area1];

    
    UILabel *area2 = [[UILabel alloc] initWithFrame:CGRectMake(area1.frame.origin.x + area1.frame.size.width + 15 * ScreenWidth, area1.frame.origin.y , area1.frame.size.width, area1.frame.size.height)];
    area2.text = @"三号场地";
    area2.textColor = [UIColor colorWithRed:30 / 255.0 green:30 / 255.0 blue:30 / 255.0 alpha:1.0];
    [self.view addSubview:area2];


    //滑动scroview
    self.scroV = [[UIScrollView alloc] initWithFrame:CGRectMake(70, 150, [UIScreen mainScreen].bounds.size.width , self.view.frame.size.height - 150)];
    self.scroV.pagingEnabled = YES;
    self.scroV.showsVerticalScrollIndicator = NO;
    self.scroV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 5 + 100,  0);
    
    [self.view addSubview:self.scroV];

    
    
    
    //横向collectionView 时间
    UICollectionViewFlowLayout *flowL_B = [[UICollectionViewFlowLayout alloc] init];
    //最小行间距
    flowL_B.minimumLineSpacing = 10 * ScreenWidth;
    //滚动方向
    flowL_B.scrollDirection = UICollectionViewScrollDirectionVertical;
    //单位大小
    flowL_B.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width - 70 * ScreenWidth) / 4, 35);
    //与屏幕四边的间距
    flowL_B.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.BookcollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * 5, [UIScreen mainScreen].bounds.size.height - 150) collectionViewLayout:flowL_B];
    //隐藏指示条
    self.BookcollectionV.showsHorizontalScrollIndicator = NO;
    //关掉滑动
        self.HcollectionV.scrollEnabled = YES;
    //关闭自动偏移
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.BookcollectionV.backgroundColor = [UIColor colorWithRed:255 / 255.0  green:255 / 255.0 blue:255 / 255.0 alpha:1.0];
    self.BookcollectionV.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width  / 4,  0);
    //签协议
    self.BookcollectionV.delegate = self;
    self.BookcollectionV.dataSource = self;
    //注册cell
    [self.BookcollectionV registerClass:[RoundCollectionViewCell class] forCellWithReuseIdentifier:@"roundCell"];
    self.BookcollectionV.bounces = NO;
    self.BookcollectionV.pagingEnabled = YES;
    
    [self.scroV addSubview:self.BookcollectionV];
    
    //背景
    
    self.viewB = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 89, [UIScreen mainScreen].bounds.size.width , 40)];
    self.viewB.backgroundColor = [UIColor colorWithRed:25 / 255.0 green:25 / 255.0  blue:25 / 255.0  alpha:1.0];
    [self.view addSubview:self.viewB];
    [self.viewB removeFromSuperview];
    
    
    //结算按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 84, [UIScreen mainScreen].bounds.size.width - 20, 30);
    self.button.layer.cornerRadius = 5;
    self.button.layer.masksToBounds = YES;
    self.button.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:22 / 255.0  blue:35 / 255.0  alpha:1.0];
    [self.button setTitle:@"预订" forState:UIControlStateNormal];
    self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.button setTitleColor:[UIColor colorWithRed:230 / 255.0 green:230 / 255.0  blue:230 / 255.0  alpha:1.0] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    [self.button removeFromSuperview];
    

    
}

- (void)buttonAction:(UIButton *)sender
{
    
    
    
}

//点击触发
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    if (self.HcollectionV == collectionView) {
        //动画滑块 点击改变滑块位置
        [UIView animateWithDuration:0.2 animations:^{
            
            self.scrollView.frame = CGRectMake(5 * ScreenWidth + ([UIScreen mainScreen].bounds.size.width / 4) * indexPath.row, 39 * ScreenHeigth, [UIScreen mainScreen].bounds.size.width / 4 - 3 * 10, 2.5);
            
        }];

        
    } else
    {
        
        //设置选中cell属性
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        
        
        if (YES == self.clickbutton) {
            
            cell.backgroundColor = [UIColor colorWithRed:200 / 255.0 green:22 / 255.0 blue:35 / 255.0 alpha:1.0];

            [self.view addSubview:self.viewB];
            [self.view addSubview:self.button];
            self.clickbutton = NO;
        } else
        {
            cell.backgroundColor = [UIColor colorWithRed:50 / 255.0 green:50 / 255.0 blue:50 / 255.0 alpha:1.0];

            [self.button removeFromSuperview];
            [self.viewB removeFromSuperview];
            self.clickbutton = YES;
            
        }
        
        
    }
    
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.HcollectionV == collectionView) {
        
        return 4;
        
    }else
    {
        return 132;
    }
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.HcollectionV == collectionView) {
        
        TimeCollectionViewCell *hor = [collectionView dequeueReusableCellWithReuseIdentifier:@"horizontalCell" forIndexPath:indexPath];
        
        
        hor.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
        return hor;

        
    } else
    {
        RoundCollectionViewCell *round = [collectionView dequeueReusableCellWithReuseIdentifier:@"roundCell" forIndexPath:indexPath];
        
     
        
        round.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1.0];
            
            
        
        
        return round;
    }
        
    
    
}





- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
